//
//  FishController.m
//  TestDemo
//
//  Created by zzz on 15/9/9.
//  Copyright © 2015年 zzz. All rights reserved.
//

#import "FishController.h"

#define FishCount 10

@interface FishController ()

//小鱼
@property (nonatomic, strong) CALayer *fishLayer;
//小鱼帧集
@property (nonatomic, strong) NSMutableArray *fishFrames;
//当前帧
@property (nonatomic, assign) NSInteger currentFrame;
//动画是否开始
@property (nonatomic, assign, getter=isStart) BOOL start;

@end

@implementation FishController

- (CALayer *)fishLayer{
    if (_fishLayer == nil) {
        _fishLayer = [CALayer layer];
    }
    return _fishLayer;
}

- (NSMutableArray *)fishFrames{
    if (_fishFrames == nil) {
        _fishFrames = [[NSMutableArray alloc] initWithCapacity:FishCount];
    }
    return _fishFrames;
}


- (IBAction)StartBtnClick:(UIButton *)sender {
    
    static dispatch_once_t t;
    dispatch_once(&t, ^{
        //路径动画
        CAKeyframeAnimation *pathAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(170, 240) radius:130 startAngle:M_PI_2 * 3 endAngle:-M_PI_2 clockwise:NO];
        pathAnim.path = path.CGPath;
        
        //旋转动画
        CAKeyframeAnimation *rotationAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        rotationAnim.values = @[
                                [NSValue valueWithCATransform3D:CATransform3DIdentity],
                                [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI_2, 0, 0, 1)],
                                [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI, 0, 0, 1)],
                                [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI_2 * 3, 0, 0, 1)],
                                [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI * 2, 0, 0, 1)],
                                ];
        CAAnimationGroup *animGroup = [CAAnimationGroup animation];
        animGroup.animations = @[pathAnim, rotationAnim];
        animGroup.duration = 10;
        animGroup.repeatCount = 10;
        [self.fishLayer addAnimation:animGroup forKey:@"move"];
        self.start = YES;
    });
    
    if (sender.selected) {
        CFTimeInterval pauseTime = [self.fishLayer convertTime:CACurrentMediaTime() fromLayer:nil];
        self.fishLayer.timeOffset = pauseTime;
        self.fishLayer.speed = 0;
        sender.selected = NO;
    } else{
        if (self.isStart) {
            CFTimeInterval pausedTime = [self.fishLayer timeOffset];
            self.fishLayer.speed = 1.0;
            self.fishLayer.timeOffset = 0.0;
            self.fishLayer.beginTime = 0.0;
            CFTimeInterval timeSincePause = [self.fishLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
            self.fishLayer.beginTime = timeSincePause;
        }
        sender.selected = YES;
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置CALayer
    self.fishLayer.frame = CGRectMake(125, 90, 90, 40);
    self.fishLayer.contentsGravity = kCAGravityCenter;
    [self.view.layer addSublayer:self.fishLayer];
    //加载图片
    for (int i = 0; i < FishCount; i++) {
        [self.fishFrames addObject:[UIImage imageNamed:[NSString stringWithFormat:@"fish%d", i]]];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(fishMove) userInfo:nil repeats:YES];
}

- (void)fishMove{
    self.fishLayer.contents = (__bridge id)[[self.fishFrames objectAtIndex:self.currentFrame++ % 10] CGImage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
